package com.aaa.power.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.entity.User;
import com.aaa.power.mapper.UserMapper;
import com.aaa.power.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户信息表(User)表服务实现类
 *
 * @author YuXin
 * @since 2020-11-11 18:35:18
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer userId) {
        return this.userMapper.queryById(userId);
    }

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param user 实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(Integer pageNo, Integer pageSize, User user) {
        if (pageNo == 0 || pageSize == 0) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<User> pageInfo = new PageInfo<>(userMapper.queryAll(user));
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), pageInfo);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        String salt = "efdd1d36-2430-4";
        user.setSalt(salt);
        String password = user.getPassword();
        Sha512Hash sha512Hash = new Sha512Hash(password, salt, 1024);
        System.out.println(sha512Hash.toString());
        user.setPassword(sha512Hash.toString());
        this.userMapper.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        String salt = "efdd1d36-2430-4";
        user.setSalt(salt);
        String password = user.getPassword();
        Sha512Hash sha512Hash = new Sha512Hash(password, salt, 1024);
        System.out.println(sha512Hash.toString());
        user.setPassword(sha512Hash.toString());
        this.userMapper.update(user);
        return this.queryById(user.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.userMapper.deleteById(userId) > 0;
    }


    /**
     * sso远程调用此方法
     *
     * @param userName
     * @param userName
     * @return
     */
    @Override
    public User queryUserByParam(String userName) {
        return userMapper.queryUserByUserName(userName);
    }

    /**
     * 查询用户id对应的角色id集合
     *
     * @param userId
     * @return
     */
    @Override
    public Result selectRoleIdsByUserId(Integer userId) {
        List<Integer> menuList = userMapper.selectRoleIdsByUserId(userId);
        List roleIdList = new ArrayList();
        for (Integer i : menuList) {
            roleIdList.add(i);
        }
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), roleIdList);
    }

    /**
     * 删除原有用户ID关联的所有角色
     *
     * @param userId
     * @return
     */
    @Override
    public int deleteRoleIdsByUserId(int userId) {
        return userMapper.deleteRoleIdsByUserId(userId);
    }

    /**
     * 添加用户角色关联
     *
     * @param userId
     * @param roleIds
     * @return
     */
    @Override
    public int addUserAndRole(int userId, String roleIds) {
        try {
            //调用上面方法，删除该用户关联的所有角色
            this.deleteRoleIdsByUserId(userId);
            StringBuffer stringBuffer = new StringBuffer();
            //分割字符串，循环拼接values后面  (1,1),(1,2),(1,10)...
            String[] ridArray = roleIds.split(",");
            for (String rid : ridArray) {
                stringBuffer.append("(" + userId + "," + rid + "),");
            }
            String values = stringBuffer.substring(0, stringBuffer.length() - 1);
            userMapper.addUserAndRole(values);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}